 <template>
   <Card :shadow="shadow">
      <div class="card-name">
        <div class="name width1">用户列表</div>
      </div>
      <div class="content-con">
        <div class="bank-search">
          <Form class="search-item">

            <FormItem class="item-padding">
              <i-input size="large" v-model="keyword" placeholder="ID|用户名"></i-input>
            </FormItem>

            <FormItem>
              <i-button size="large" class="btn-green" @click="search">搜索</i-button>
            </FormItem>
          </Form>
        </div>
        <div class="bank-add">
          <i-button size="large" @click="userAdd" class="btn-green item-margin">新增</i-button>
          <!-- <i-button size="large" @click="remove()" class="btn-green">删除</i-button> -->
        </div>
        <div class="bank-form">
          <div class="form-item">
            <i-table :columns="foodColumns" :data="foodData">
              <template slot-scope="{ row }" slot="account">
                {{row.account}}
                <tag color="warning" v-if="row.level==0">
                   {{row.level | adminLevel}}
                </tag>

              </template>
              <template slot-scope="{ row }" slot="status">
                <tag v-if="row.status==1"  color="success">{{row.status | adminStatus}} </tag>
                <tag v-else  color="error">{{row.status | adminStatus}} </tag>
              </template>
            </i-table> <!-- table组件 -->
          </div>
          <div class="page">
            <Page :id="pageId"
             :total="dataCount"
             :page-size="pageSize"
             style="float: left;"
             @on-change="changepage"
             @on-page-size-change="nowPageSize"
             show-total show-sizer show-elevator></Page> <!-- page组件 -->
            <Button style="float: left;margin-left: 10px;" type="primary" @click="goElevatorPage">确认</Button>
          </div>
        </div>
      </div>
   </Card>

 </template>
<script>
import { getAdminList, delAdmin } from '@/api/user'

export default {
  // name: 'user_management',
  props: {
    shadow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      keyword: '',
      value: '',
      pageId: 'targetpage',
      ajaxHistoryData: [],
      exportdata: [],
      // 初始化信息总条数
      dataCount: 0,
      // 每页显示多少条
      pageSize: 10,
      pageCurrent: 1, // 当前页
      foodColumns: [
        {
          title: '用户ID',
          key: 'id'
        },
        {
          title: '用户名',
          key: 'account',
          slot: 'account'
        },
        {
          title: '用户昵称',
          key: 'real_name'
        },
        {
          title: '上次登录IP',
          key: 'last_ip'
        },
        {
          title: '上次登录时间',
          key: 'last_time'
        },
        {
          title: '状态',
          key: 'status',
          slot: 'status'
        },
        {
          title: '创建时间',
          key: 'add_time'
        },
        {
          title: '操作',
          key: 'action',
          width: 300,
          align: 'center',

          render: (h, params) => {
            return h('div', [

              h(
                'Button', {
                  props: {
                    size: 'small'
                  },
                  style: {
                    color: 'white',
                    padding: '0 5px',
                    height: '24px',
                    marginLeft: '15px',
                    fontSize: '12px',
                    backgroundColor: '#009688'
                  },
                  on: {
                    click: () => {
                      this.$router.push({
                        name: 'user-edit',
                        params: {
                          row: params.row
                        }
                      })
                    }
                  }
                },
                '编辑'
              ),
              h(
                'Button', {
                  props: {
                    size: 'small'
                  },
                  style: {
                    color: 'white',
                    padding: '0 5px',
                    height: '24px',
                    marginLeft: '15px',
                    fontSize: '12px',
                    backgroundColor: '#ff5500'
                  },
                  on: {
                    click: () => {
                      this.delUser(params.row.id)
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      foodData: []
    }
  },
  methods: {
    getList () {
      let data = {
        keyword: this.keyword,
        pageCurrent: this.pageCurrent,
        pageSize: this.pageSize
      }
      getAdminList(data)
        .then(res => {
          this.foodData = res.data.data
          this.dataCount = res.data.count
        }).catch(err => {
          this.$message.error(err.msg)
        })
    },
    search () {
      this.getList()
    },

    // 点击切换页面
    changepage (index) {
      this.pageCurrent = index
      this.getList()
    },
    // 每页显示的数据条数
    nowPageSize (index) {
      // 实时获取当前需要显示的条数
      this.pageSize = index
      this.getList()
    },

    // 点击确认，跳转到输入的页码
    goElevatorPage () {
      var evtObj
      var thisPage = document.getElementById(this.pageId)
      // var thisPage=this.$refs.myPage;//也可以用ref来获取page这个dom
      var elevatorDiv = thisPage.getElementsByClassName('ivu-page-options-elevator')
      // 如果thisPage这里报错了,请核对上文的获取page的DOM的语句是否获取不到
      if (elevatorDiv && elevatorDiv.length > 0) {
        var pageInput = elevatorDiv[0].getElementsByTagName('input')[0]
        if (window.KeyEvent) { // firefox 浏览器下模拟事件
          evtObj = document.createEvent('KeyEvents')
          evtObj.initKeyEvent('keyup', true, true, window, true, false, false, false, 13, 0)
        } else { // chrome 浏览器下模拟事件
          evtObj = document.createEvent('UIEvents')
          evtObj.initUIEvent('keyup', true, true, window, 1)
          delete evtObj.keyCode
          if (typeof evtObj.keyCode === 'undefined') { // 为了模拟keycode
            Object.defineProperty(evtObj, 'keyCode', { value: 13 })
          } else {
            evtObj.key = String.fromCharCode(13)
          }
        }
        pageInput.dispatchEvent(evtObj)
      }
    },
    // 新增按钮
    userAdd () {
      this.$router.push({ name: 'user-add' })
    },

    // 删除用户时，通过splice(index, 1)，可以删除index位置的数据，页面上的数据自动就会更新
    delUser (id) {
      this.$Modal.confirm({
          title:'确认信息',
          content:'确认删除？',
          okText: '确认',
          cancelText: '取消',
          onOk:async()=>{ 
			delAdmin(id)
			  .then(res => {
				this.$Message.success(res.msg)
				this.getList()
			  })
			  .catch(res => {
				this.$Message.error(res.msg)
			  })
		  }
      })
    }
  },
  created () {
    this.getList()
  }
}
</script>
 <style lang="less">

 </style>
